#!/bin/bash

if [[ $# -ne 2 ]]; then
    echo "The format is as follows:"
    echo "bash charm-6.9.0.sh buildpath installpath"
    echo ":param buildpath: 应用构建绝对路径"
    echo ":param installpath: 应用安装绝对路径"
    exit 1
fi

if [[ ! "$1" =~ ^/.* || ! -d "$1" || "$1" = "/" ]]; then echo "请输入正确的构建路径"; exit 1; fi
if [[ ! "$2" =~ ^/.* || ! -d "$2" || "$2" = "/" ]]; then echo "请输入正确的安装路径"; exit 1; fi

buildpath=$1
installpath=$2
if [[ "$1" =~ .*/$ ]]; then buildpath=${buildpath%/*}; fi
if [[ "$2" =~ .*/$ ]]; then installpath=${installpath%/*}; fi
[[ "$buildpath" == "$installpath" ]] && echo "构建路径与安装路径需为不同路径" && exit 1

set -e
export CC=clang
export CXX=clang++ 
export FC=flang

wget -t 0 -c -P $buildpath https://charm.cs.illinois.edu/distrib/charm-6.9.0.tar.gz
tar -zxvf $buildpath/charm-6.9.0.tar.gz -C $buildpath && rm -rf $buildpath/charm-6.9.0.tar.gz
cd $buildpath/charm-6.9.0

mkdir $installpath/CHARM/
cp -rf src/arch/mpi-linux-x86_64 src/arch/mpi-linux-arm8
grep -rl 'm64' src/arch/mpi-linux-arm8 | xargs sed -i 's/\-m64//g'
./build charm++ mpi-linux-arm8 smp --with-production --destination=$installpath/CHARM/mpi-linux-arm8

